<?php
/**
    c2earn 视频处理类
	@author xiaoming<maixiaoming@9qi.cn>
*/
class VideoAction extends BaseAction
{

	
    /**
		定义空操作
	*/
	public function _empty()
	{
		
		$this->_pageNotFound();
	}
	
	/**
		视频管理首页
	*/
	public function index() 
	{
		import("ORG.Util.Page"); // 导入分页类
		
		$Video = D('Video');
		
		$condition = "is_show=1 ";
		$count = $Video->where($condition)->count();
		$Page = new Page($count,10);
		$show = $Page->show(); // 分页显示输出
		
		$videos = $Video->where($condition)
						 ->order('video_id DESC')
						 ->limit($Page->firstRow.','.$Page->listRows)->select();
		
		foreach ($videos as &$video) {
			$video['editUrl'] = U("Admin://video/edit?id={$video['video_id']}");
			$video['deleteUrl'] = U("Admin://video/delete?id={$video['video_id']}");
		}
		
		//獲取分類列表
		$Cat = M('video_cat');
		$cat_list = $Cat->select();
		$cats = array();
		foreach($cat_list as $k=>$v) {
			$cats[$v['cat_id']] = $v['cat_name'];
		}

		$this->assign('cats',$cats);
		
		$this->assign('page',$show);
		$addUrl = U("Admin://video/add");
		$this->assign('addUrl',$addUrl);
		
		$this->assign('videos',$videos);
		$this->assign('nav', 'video');
		$this->display();
	}
	
	/**
	    添加视频
	*/
	public function add()
	{
		if ($_POST['submitted']) {

		    //增加视频操作
			$Video = D('Video');
			if(!$Video->create()){
				$url = $_SERVER["HTTP_REFERER"];
				$this->_message('error!',$url,$Video->getError());
		
			} else{
				import("ORG.Net.UploadFile");

				if(!empty($_FILES['photo1']['name']) || !empty($_FILES['photo2']['name'])){
				
					import("ORG.Net.UploadFile");
					$upload = new UploadFile(); // 实例化上传类   
					$upload->maxSize  = 30145728 ; // 设置附件上传大小   
					$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg', 'flv'); // 设置附件上传类型   
					$upload->savePath =  '../Public/Uploads/video/'; // 设置附件上传目录
					$upload->uploadReplace = true; //同名覆蓋	
						//上传图片处理

						if(!$upload->upload()) { // 上传错误 提示错误信息   
							$this->error($upload->getErrorMsg());   
						} else { // 上传成功 获取上传文件信息   
						
							$info =  $upload->getUploadFileInfo();
							
							if (!empty($_FILES['photo1']['name']) && empty($_FILES['photo2']['name'])) {
								$Video->image_name = $info[0]["savename"];
							} else if (empty($_FILES['photo1']['name']) && !empty($_FILES['photo2']['name'])) {
								$Video->video_url = $info[0]["savename"];
							} else if (!empty($_FILES['photo1']['name']) && !empty($_FILES['photo2']['name'])) {
								$Video->image_name = $info[0]["savename"];
								$Video->video_url = $info[1]["savename"];
							}

							$Video->image_path = '/Public/Uploads/video/';					
						}  

				}
			
				$Question = M('Question');
				$Answer = M('Answer');
				
				//插入问题数据
				$Question->create();
				$q_id = $Question->add();
				
				//插入答案数据
				$correct_id = 0;
			    foreach($_POST['a_name'] as $a_name){
				    if(!empty($a_name)) {
						$Answer->a_name = $a_name;
						$Answer->q_id = $q_id;
						$a_id = $Answer->add();
						$correct_id = empty($correct_id) ? $a_id : $correct_id;
					
					}

				}
				
				//更新问题数据
				$data['a_id'] = $correct_id;
				$Question->where("q_id=$q_id")->save($data);
				
				$Video->create_time = time();
				$max_counts = intval($Video->max_counts);
				$Video->max_counts = empty($max_counts) ? -1 :  $max_counts;
				$Video->q_id = $q_id;
				
				if($Video->add()) {
					echo "<script>alert('Add Successfully！');</script>";
					$url = U("Admin://video/index");
					$this->redirect($url);
				} else {
					echo "<script>alert('Add failed！');</script>";
					echo "<script>history.go(-1);</script>";
					exit;
				}

			}

		} else{
			//显示增加视频操作页面
			
			//獲取分類列表
			$Cat = M('video_cat');
			$cats = $Cat->select();
			$this->assign('cats',$cats);
			
			$this->assign('title','add a video');
			$this->assign('nav', 'video');
			$this->display();
		
		}
		

	}
	
	/**
		编辑视频
	*/
	public function edit()
	{
		$id = intval($_GET['id']);
		$Video = D('Video');
		$Question = M('Question');
		$Answer = M('Answer');

		if ($_POST['submitted']) {
			if(!$Video->create()){
			    $url = $_SERVER["HTTP_REFERER"];
				$this->_message('error!',$url,$Video->getError());
			
			} else {

				if(!empty($_FILES['photo1']['name']) || !empty($_FILES['photo2']['name'])){
				
					import("ORG.Net.UploadFile");
					$upload = new UploadFile(); // 实例化上传类   
					$upload->maxSize  = 30145728 ; // 设置附件上传大小   
					$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg', 'flv'); // 设置附件上传类型   
					$upload->savePath =  '../Public/Uploads/video/'; // 设置附件上传目录
					$upload->uploadReplace = true; //同名覆蓋	
						//上传图片处理

						if(!$upload->upload()) { // 上传错误 提示错误信息   
							$this->error($upload->getErrorMsg());   
						} else { // 上传成功 获取上传文件信息   
						
							$info =  $upload->getUploadFileInfo();
							
							if (!empty($_FILES['photo1']['name']) && empty($_FILES['photo2']['name'])) {
								$Video->image_name = $info[0]["savename"];
							} else if (empty($_FILES['photo1']['name']) && !empty($_FILES['photo2']['name'])) {
								$Video->video_url = $info[0]["savename"];
							} else if (!empty($_FILES['photo1']['name']) && !empty($_FILES['photo2']['name'])) {
								$Video->image_name = $info[0]["savename"];
								$Video->video_url = $info[1]["savename"];
							}
								
							$Video->image_path = '/Public/Uploads/video/';					
						}  

				}

				$Question->create();
				$Question->save();
							    
				$Video->save();
				
				//修改问题答案
				foreach($_POST['a_name'] as $k=>$v) {    //修改现有的问题
					$data['a_id']   = $k;
					$data['a_name'] = $v;
					$Answer->save($data);
				}
				
				//新增问题答案
				foreach($_POST['a_name1'] as $a_name){
					if(!empty($a_name)) {
						$Answer->a_name = $a_name;
						$Answer->q_id = $_POST['q_id'];
						$Answer->add();
					}
				}
				//操作成功，跳转页面
				//$url = U("Admin://video/edit?id={$_POST['video_id']}");
				//$this->redirect("video/edit", array('id'=>$_POST['video_id']), 3, '操作成功');
				echo "<script>alert('Edit Successfully！');</script>";
				$this->redirect("video/edit", array('id'=>$_POST['video_id']));
			}	
			
		
		} else {
			$result = $Video->where("video_id=$id")->find();
			if ($result) {
				
				$q_info = $Question->where("q_id={$result['q_id']}")->find();
				$answers = $Answer->where("q_id={$result['q_id']}")->select();
				
				for($i = 0; $i<5; $i++){
					if(!empty($answers[$i])) {
						$answers[$i]['a_id'] = "a_name[{$answers[$i]['a_id']}]";
						continue;
					}
					
					$answers[$i]['a_id']   = 'a_name1[]';
					$answers[$i]['a_name'] = '';
				
				}
				
				//獲取分類列表
				$Cat = M('video_cat');
				$cats = $Cat->select();
				$this->assign('cats',$cats);
				$this->assign('c_cat',$result['cat_id']);

				$this->assign('q_info',$q_info);
				$this->assign('answers',$answers);
				
				//$this->assign('a_num',5-count($answers));
				
			    $result['max_counts'] = ($result['max_counts'] < 0) ? '' : $result['max_counts'];
			    $this->assign('title','Edit Video');
				$this->assign('video',$result);
				$this->assign('nav', 'video');
				$this->display();
			} else {
			    $url = U('Admin://video/add');
				$this->_message('error!',$url,'Page Not Found!');

			}
			
		}
	}
	
	/**
		删除广告视频
	*/
	public function delete()
	{
		$Video = M('video');
		$id = intval($_GET['id']);
		$result = $Video->where("video_id=$id")->delete();
		if($result) {
			$this->redirect("video/index");
		} else {
			$this->redirect("video/index",array(),3, '删除失败');
		}
	}
}

?>
